package com.google.shop.product.common.utils;

import io.jsonwebtoken.Header;
import io.jsonwebtoken.JwsHeader;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.springframework.util.StringUtils;

/* loaded from: app.jar:BOOT-INF/lib/shop-common-0.0.1-SNAPSHOT.jar:com/google/shop/common/utils/JwtUtils.class */
public class JwtUtils {
    public static final long EXPIRE = 86400000;
    public static final String APP_SECRET = "ukc8BDbRigUDaY6pZFfWus2jZWLPHO";

    public static String getJwtToken(String id, String nickname) {
        return Jwts.builder().setHeaderParam(Header.TYPE, Header.JWT_TYPE).setHeaderParam(JwsHeader.ALGORITHM, "HS256").setSubject("shop-user").setIssuedAt(new Date()).setExpiration(new Date(System.currentTimeMillis() + 86400000)).claim("id", id).claim("nickname", nickname).signWith(SignatureAlgorithm.HS256, APP_SECRET).compact();
    }

    public static boolean checkToken(String jwtToken) {
        if (StringUtils.isEmpty(jwtToken)) {
            return false;
        }
        try {
            Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(jwtToken);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean checkToken(HttpServletRequest request) {
        try {
            String jwtToken = request.getHeader("shoptoken");
            if (StringUtils.isEmpty(jwtToken)) {
                return false;
            }
            Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(jwtToken);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static String getMemberIdByJwtToken(String shoptoken) {
        return StringUtils.isEmpty(shoptoken) ? "" : (String) Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(shoptoken).getBody().get("id");
    }

    public static String getMemberEmailByJwtToken(String shoptoken) {
        return StringUtils.isEmpty(shoptoken) ? "" : (String) Jwts.parser().setSigningKey(APP_SECRET).parseClaimsJws(shoptoken).getBody().get("nickname");
    }
}
